package com.example.appstudy2405;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.example.appstudy2405.dto.ViewHoldDto;

public class BitCatP22ListView extends AppCompatActivity {

    private String[] titles = {"桌子", "SUV", "蛋糕", "苹果", "毛线衣", "猕猴桃", "茶杯"};

    private String[] prices = {"200 元", "150000 元","150 元","9.9 元","55 元","15.5 元", "35 元"};

    private int[] icons = {R.drawable.desk, R.drawable.suv, R.drawable.cake, R.drawable.apple,
            R.drawable.mxy, R.drawable.mht, R.drawable.cup};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bit_cat_p22_list_view);

        // 1. get ListView
        ListView listView = findViewById(R.id.lv_demo);
        MyAdpter myAdpter = new MyAdpter();
        listView.setAdapter(myAdpter);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(
                        BitCatP22ListView.this,
                        "标题：" + titles[i] + ", 价格" + prices[i],
                        Toast.LENGTH_SHORT
                ).show();
            }
        });

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(
                        BitCatP22ListView.this,
                        "[长长长长长长长长长长长长长长长长长按事件] 标题：" + titles[i] + ", 价格" + prices[i],
                        Toast.LENGTH_SHORT
                ).show();
                return true;
            }
        });
    }



    class MyAdpter extends BaseAdapter {

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public Object getItem(int i) {
            return titles[i];
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHoldDto holder;
            if (view == null) {
                view = View.inflate(BitCatP22ListView.this, R.layout.p22_item_list, null);
                holder = new ViewHoldDto();

                holder.iv_icon = view.findViewById(R.id.iv);
                holder.tv_title = view.findViewById(R.id.tv_title);
                holder.tv_price = view.findViewById(R.id.tv_price);
                view.setTag(holder);
            } else {
                holder = (ViewHoldDto) view.getTag();
            }

            holder.iv_icon.setImageResource(icons[i]);
            holder.tv_title.setText(titles[i]);
            holder.tv_price.setText(prices[i]);

            return view;
        }
    }
}